package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Reservation;
import com.hengxinguotong.zhihuichengjian.ui.engineering.reservation.AddReservationActivity;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/27.
 */

public class ReservationAdapter extends BaseAdapter {
    private List<Reservation> reservations;
    private Context context;

    public ReservationAdapter(Context context, List<Reservation> reservations) {
        this.context = context;
        this.reservations = reservations;
    }

    public void setDatas( List<Reservation> reservations){
        this.reservations = reservations;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return reservations.size();
    }

    @Override
    public Reservation getItem(int position) {
        return reservations.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_reservation, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Reservation reservation = reservations.get(position);
        if(null == reservation.getRemark() || Utils.isEmpty(reservation.getRemark())){
            holder.content.setText("无备注信息");
        }else{
            holder.content.setText(reservation.getRemark());
        }
        holder.deviceTv.setText(AddReservationActivity.devices[reservation.getEquipmentType() - 1]);
        holder.dataTv.setText(reservation.getCreateTime());
        if(null == reservation.getProjectCompanyName() || Utils.isEmpty(reservation.getProjectCompanyName())){
            holder.companyTv.setText("无分包公司");
        }else{
            holder.companyTv.setText(reservation.getProjectCompanyName());
        }
        holder.personTv.setText(reservation.getAppointmentName());
        if (reservation.getIsUrgentTask() == 1) {
            holder.urgentTv.setVisibility(View.VISIBLE);
        } else {
            holder.urgentTv.setVisibility(View.GONE);
        }

        if ("1".equals(reservation.getStatus())) {
            holder.stateTv.setText("待处理");
            holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_dd66ff));
            holder.stateIv.setImageResource(R.mipmap.task_daishouli);
        } else if ("2".equals(reservation.getStatus())) {
            holder.stateTv.setText("待审核");
            holder.stateIv.setImageResource(R.mipmap.task_daijiancha);
            holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_1294db));
        } else if ("3".equals(reservation.getStatus())) {
            holder.stateTv.setText("处理中");
            holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_5dd7e2));
            holder.stateIv.setImageResource(R.mipmap.task_chulizhong);
        } else if ("4".equals(reservation.getStatus())) {
            holder.stateTv.setText("审核不通过");
            holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_fc8d43));
            holder.stateIv.setImageResource(R.mipmap.dispatched_workers);
        } else {
            holder.stateTv.setText("已完成");
            holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_71e5a2));
            holder.stateIv.setImageResource(R.mipmap.already_confirmed);
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.state_iv)
        ImageView stateIv;
        @BindView(R.id.state_tv)
        HXTextView stateTv;
        @BindView(R.id.urgent_tv)
        HXTextView urgentTv;
        @BindView(R.id.content)
        HXTextView content;
        @BindView(R.id.device_tv)
        HXTextView deviceTv;
        @BindView(R.id.person_tv)
        HXTextView personTv;
        @BindView(R.id.company_tv)
        HXTextView companyTv;
        @BindView(R.id.data_tv)
        HXTextView dataTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
